package com.lan.app.service.impl;

import java.util.Date;
import java.util.List;

import com.lan.app.dao.TplDao;
import com.lan.app.repository.Tpl;
import com.lan.app.service.ITplService;
import com.lan.support.response.ex.Ex;
import com.lan.support.page.Page;
import com.lan.support.page.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class TplService implements ITplService {

    @Resource
    private TplDao tplDao;

    @Override
    public Integer save(Tpl tpl) {
        Date curDate = new Date();
        tpl.setIsDel(1);
        tpl.setCreateTime(curDate);
        tpl.setUpdateTime(curDate);
        int result = tplDao.save(tpl);
        Integer id = tpl.getId();
        return id;
    }

    @Override
    public int modifyDel(Integer id) {
        int result = tplDao.modifyDel(id, 0, new Date());
        if (result == 0) {
            throw Ex.build("删除失败");
        }
        return result;
    }

    public int updateById(Tpl tpl) {
        if (tpl.getId() == null) {
            throw Ex.build("参数不能为空");
        }
        int result = tplDao.updateById(tpl);
        return result;
    }

    @Override
    public Tpl get(Integer id) {
        Tpl tpl = tplDao.findById(id);
        return tpl;
    }

    @Override
    public Page<Tpl> getByPage(PageHelper helper, Tpl tpl) {
        int count = tplDao.countByPage(tpl, helper.getPage());
        Page<Tpl> page = new Page<>(helper, count);
        if (count == 0) {
            page.setContent(null);
        } else {
            List<Tpl> list = tplDao.findByPage(tpl, helper.getPage());
            page.setContent(list);
        }
        return page;
    }
}